package com.zhuxiang.test5;

import java.io.*;
import java.net.Socket;

/**
 * 客户端发送信息线程类
 */
public class ClientSendThread extends Thread {

    private Socket s = null;

    public ClientSendThread(Socket s){
        this.s = s;
    }

    @Override
    public void run(){
       BufferedReader br =  null;
       PrintStream ps = null;

        try {
            ps = new PrintStream(s.getOutputStream());
            while (true){
                System.out.println("请输入聊天的内容，输入file发送文件");
                br = new BufferedReader(new InputStreamReader(System.in));
                String str1 = br.readLine();
                //根据str1的值判断执行什么操作
                if ("bye".equalsIgnoreCase(str1)){
                    System.out.println("over");
                    break;
                }else if ("file".equalsIgnoreCase(str1)){
                    ps.println(str1);
                    System.out.println("请输入发送文件的路径");
                    String filePath = br.readLine();
                    new ClientSendFileThread(s,filePath).start();
                }else {
                    ps.println(str1);
                    System.out.println("客户端发送数据成功");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != ps){
                ps.close();
            }
            if (null != br){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
